package com.techsoul.module.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.basic.pojo.entity.TbWmsContainBoxType;
import com.techsoul.module.basic.pojo.param.TbWmsContainBoxTypeSaveParam;
import com.techsoul.module.basic.service.ITbWmsContainBoxTypeService;
import com.techsoul.module.basic.service.ITmsCustomerService;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.module.basic.mapper.TmsCustomerMapper;
import com.techsoul.module.basic.pojo.entity.TmsCustomer;
import com.techsoul.module.basic.pojo.param.TmsCustomerSaveParam;
import com.techsoul.module.basic.pojo.param.TmsCustomerSearchParam;
import com.techsoul.module.basic.pojo.vo.TmsCustomerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author liuen
 * @since 2019-11-09
 */
@Service
@Transactional(readOnly = true)
public class TmsCustomerServiceImpl implements ITmsCustomerService {

    @Autowired
    private TmsCustomerMapper mapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IdWorker idWorker;

    @Transactional(readOnly = false)
    @Override
    public String insert(TmsCustomerSaveParam params) {
        TmsCustomer entity = new TmsCustomer();
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }

    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {

            return this.mapper.deleteById(id);
    }
        /**
         * 修改
         * @param params
         * @return
         */
    @Transactional(readOnly = false)
    @Override
    public Integer update(TmsCustomerSaveParam params) {
            TmsCustomer entityFromDb = this.mapper.selectById(params.getId());
            if (entityFromDb == null) {
                throw new ParamException("id=" + params.getId());
            }
            UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
            com.techsoul.common.core.util.bean.BeanUtils.copyProperties(params, entityFromDb);
            entityFromDb.preUpdate(sysUser.getId());
            return this.mapper.updateById(entityFromDb);
        }


        public TmsCustomer findById(String id) {
        return this.mapper.selectById(id);
    }

    public List<TmsCustomer> findAll() {
        return this.mapper.selectList(new QueryWrapper<>());
    }

    public List<TmsCustomerVo> findAll(TmsCustomerSearchParam params) {
        return this.mapper.findAll(params);
    }

    public IPage<TmsCustomerVo> findAll(TmsCustomerSearchParam params, Integer currentPage, Integer pageSize) {
        return this.mapper.findAll(new Page<>(currentPage, pageSize), params);
    }

}
